use serde_derive::Serialize;

#[derive(Debug, Serialize)]
pub struct ResponseCode {
    pub code: u16,
    pub msg: String,
    pub error: String,
}

#[derive(Debug)]
pub enum ResponseCodeEnum {
    // HTTP 状态码
    SUCCESS,
    BAD_REQUEST,
    UNAUTHORIZED,
    FORBIDDEN,
    NOT_FOUND,
    METHOD_NOT_ALLOWED,
    CONFLICT,
    UNSUPPORTED_MEDIA_TYPE,
    UNPROCESSABLE_ENTITY,
    INTERNAL_SERVER_ERROR,
    SERVICE_UNAVAILABLE,

    // 登录相关
    LOGIN_ERR_PWD,
    LOGIN_TIMEOUT,
    ERROR_TOKEN,
    OTHER_LOGIN,
    ERR_PERMISSION,
    ACCOUNT_LOCKED,
    ACCOUNT_DISABLED,
    CAPTCHA_ERROR,

    // 数据库相关
    DB_ERROR,
    DB_CONNECTION_ERROR,
    QUERY_ERROR,
    INSERT_ERROR,
    UPDATE_ERROR,
    DELETE_ERROR,
    TRANSACTION_ERROR,
    UNIQUE_CONSTRAINT_ERROR,
    FOREIGN_KEY_CONSTRAINT_ERROR,
    DATA_TYPE_ERROR,
    DATA_FORMAT_ERROR,
    DUPLICATE_KEY_ERROR,
    NO_ROWS_UPDATED,
    NO_ROWS_DELETED,
    ID_NOT_FOUND,

    // 参数验证相关
    NOT_NULL,
    NO_PARAMETER,
    PARAMETER_ERROR,
    INVALID_VALUE_ERROR,
    TYPE_ERROR,
    VALUE_TOO_LARGE,
    VALUE_TOO_SMALL,
    VALUE_OUT_OF_RANGE,
    INVALID_FORMAT,
    MISSING_FIELD,

    // 文件上传相关
    FILE_NOT_FOUND,
    ERROR_FILE_TYPE,
    OVER_SIZE,
    UPLOAD_FAILED,
    FILE_ALREADY_EXISTS,
    UNSUPPORTED_FILE_ENCODING,

    // 业务逻辑相关
    BUSINESS_ERROR,
    OPERATION_FAILED,
    DATA_CONFLICT,
    RESOURCE_LOCKED,
    DEPENDENCY_ERROR,
    INVALID_OPERATION,
    RESOURCE_EXHAUSTED,
    PRECONDITION_FAILED,
    FEATURE_NOT_IMPLEMENTED,
    RESOURCE_LIMIT_REACHED,
    BUSINESS_VALIDATION_FAILED,
    DUPLICATE_REQUEST,
    RATE_LIMIT_EXCEEDED,
    UNSUPPORTED_OPERATION,
    ILLEGAL_ARGUMENT,
    OPERATION_NOT_SUPPORTED,

    // 服务调用相关
    SERVICE_TIMEOUT,
    SERVICE_ERROR,
    GATEWAY_ERROR,
    NETWORK_ERROR,
    EXTERNAL_SERVICE_ERROR,
    DEPENDENCY_SERVICE_ERROR,
    THIRD_PARTY_SERVICE_TIMEOUT,
    THIRD_PARTY_SERVICE_ERROR,
    INVALID_SERVICE_RESPONSE,
    SERVICE_RATE_LIMIT_EXCEEDED,
    SERVICE_NOT_FOUND,
    SERVICE_NOT_CONFIG,
    SERVICE_NOT_IMPLEMENTED,

    // 独立的网关状态码
    GATEWAY_TIMEOUT,
    BAD_GATEWAY,

    // 其他
    UNKNOWN_ERROR,
    MAINTENANCE,
    OPERATION_NOT_ALLOWED,
    REQUEST_TIMEOUT,
    NOT_IMPLEMENTED,
}

impl ResponseCodeEnum {
    pub(crate) fn to_response(&self) -> ResponseCode {
        match self {
            // HTTP 状态码
            ResponseCodeEnum::SUCCESS => ResponseCode { code: 200, msg: "成功".into(), error: "None".into() },
            ResponseCodeEnum::BAD_REQUEST => ResponseCode { code: 400, msg: "请求错误".into(), error: "Bad Request".into() },
            ResponseCodeEnum::UNAUTHORIZED => ResponseCode { code: 401, msg: "未授权".into(), error: "Unauthorized".into() },
            ResponseCodeEnum::FORBIDDEN => ResponseCode { code: 403, msg: "禁止访问".into(), error: "Forbidden".into() },
            ResponseCodeEnum::NOT_FOUND => ResponseCode { code: 404, msg: "没有找到相关信息".into(), error: "Not Found".into() },
            ResponseCodeEnum::METHOD_NOT_ALLOWED => ResponseCode { code: 405, msg: "方法不允许".into(), error: "Method Not Allowed".into() },
            ResponseCodeEnum::CONFLICT => ResponseCode { code: 409, msg: "资源冲突".into(), error: "Conflict".into() },
            ResponseCodeEnum::UNSUPPORTED_MEDIA_TYPE => ResponseCode { code: 415, msg: "不支持的媒体类型".into(), error: "Unsupported Media Type".into() },
            ResponseCodeEnum::UNPROCESSABLE_ENTITY => ResponseCode { code: 422, msg: "不可处理的实体".into(), error: "Unprocessable Entity".into() },
            ResponseCodeEnum::INTERNAL_SERVER_ERROR => ResponseCode { code: 500, msg: "发生内部错误".into(), error: "Internal Server Error".into() },
            ResponseCodeEnum::SERVICE_UNAVAILABLE => ResponseCode { code: 503, msg: "服务不可用".into(), error: "Service Unavailable".into() },

            // 登录相关
            ResponseCodeEnum::LOGIN_ERR_PWD => ResponseCode { code: 400, msg: "账号或密码错误".into(), error: "Login Error Password".into() },
            ResponseCodeEnum::LOGIN_TIMEOUT => ResponseCode { code: 408, msg: "登录超时".into(), error: "Login Timeout".into() },
            ResponseCodeEnum::ERROR_TOKEN => ResponseCode { code: 401, msg: "无效的token".into(), error: "Invalid Token".into() },
            ResponseCodeEnum::OTHER_LOGIN => ResponseCode { code: 401, msg: "该账号已在其他设备登录".into(), error: "Other Login".into() },
            ResponseCodeEnum::ERR_PERMISSION => ResponseCode { code: 403, msg: "权限不足".into(), error: "Permission Denied".into() },
            ResponseCodeEnum::ACCOUNT_LOCKED => ResponseCode { code: 423, msg: "账户被锁定".into(), error: "Account Locked".into() },
            ResponseCodeEnum::ACCOUNT_DISABLED => ResponseCode { code: 403, msg: "账户被禁用".into(), error: "Account Disabled".into() },
            ResponseCodeEnum::CAPTCHA_ERROR => ResponseCode { code: 400, msg: "验证码错误".into(), error: "Captcha Error".into() },

            // 数据库相关
            ResponseCodeEnum::DB_ERROR => ResponseCode { code: 500, msg: "数据库操作失败".into(), error: "Database Error".into() },
            ResponseCodeEnum::DB_CONNECTION_ERROR => ResponseCode { code: 500, msg: "数据库连接失败".into(), error: "Database Connection Error".into() },
            ResponseCodeEnum::QUERY_ERROR => ResponseCode { code: 500, msg: "查询执行失败".into(), error: "Query Error".into() },
            ResponseCodeEnum::INSERT_ERROR => ResponseCode { code: 500, msg: "数据插入失败".into(), error: "Insert Error".into() },
            ResponseCodeEnum::UPDATE_ERROR => ResponseCode { code: 500, msg: "数据更新失败".into(), error: "Update Error".into() },
            ResponseCodeEnum::DELETE_ERROR => ResponseCode { code: 500, msg: "数据删除失败".into(), error: "Delete Error".into() },
            ResponseCodeEnum::TRANSACTION_ERROR => ResponseCode { code: 500, msg: "事务操作失败".into(), error: "Transaction Error".into() },
            ResponseCodeEnum::UNIQUE_CONSTRAINT_ERROR => ResponseCode { code: 400, msg: "唯一约束错误".into(), error: "Unique Constraint Error".into() },
            ResponseCodeEnum::FOREIGN_KEY_CONSTRAINT_ERROR => ResponseCode { code: 400, msg: "外键约束错误".into(), error: "Foreign Key Constraint Error".into() },
            ResponseCodeEnum::DATA_TYPE_ERROR => ResponseCode { code: 400, msg: "数据类型错误".into(), error: "Data Type Error".into() },
            ResponseCodeEnum::DATA_FORMAT_ERROR => ResponseCode { code: 400, msg: "数据格式错误".into(), error: "Data Format Error".into() },
            ResponseCodeEnum::DUPLICATE_KEY_ERROR => ResponseCode { code: 400, msg: "数据已存在".into(), error: "Duplicate Key Error".into() },
            ResponseCodeEnum::NO_ROWS_UPDATED => ResponseCode { code: 500, msg: "未更新任何数据".into(), error: "No Rows Updated".into() },
            ResponseCodeEnum::NO_ROWS_DELETED => ResponseCode { code: 500, msg: "未删除任何数据".into(), error: "No Rows Deleted".into() },
            ResponseCodeEnum::ID_NOT_FOUND => ResponseCode { code: 404, msg: "数据不存在".into(), error: "ID Not Found".into() },

            // 参数验证相关
            ResponseCodeEnum::NOT_NULL => ResponseCode { code: 400, msg: "参数不能为空".into(), error: "Not Null".into() },
            ResponseCodeEnum::NO_PARAMETER => ResponseCode { code: 400, msg: "缺少参数".into(), error: "No Parameter".into() },
            ResponseCodeEnum::PARAMETER_ERROR => ResponseCode { code: 400, msg: "参数错误".into(), error: "Parameter Error".into() },
            ResponseCodeEnum::INVALID_VALUE_ERROR => ResponseCode { code: 400, msg: "无效的值".into(), error: "Invalid Value Error".into() },
            ResponseCodeEnum::TYPE_ERROR => ResponseCode { code: 400, msg: "类型错误".into(), error: "Type Error".into() },
            ResponseCodeEnum::VALUE_TOO_LARGE => ResponseCode { code: 400, msg: "值太大".into(), error: "Value Too Large".into() },
            ResponseCodeEnum::VALUE_TOO_SMALL => ResponseCode { code: 400, msg: "值太小".into(), error: "Value Too Small".into() },
            ResponseCodeEnum::VALUE_OUT_OF_RANGE => ResponseCode { code: 400, msg: "值超出范围".into(), error: "Value Out of Range".into() },
            ResponseCodeEnum::INVALID_FORMAT => ResponseCode { code: 400, msg: "格式错误".into(), error: "Invalid Format".into() },
            ResponseCodeEnum::MISSING_FIELD => ResponseCode { code: 400, msg: "缺少字段".into(), error: "Missing Field".into() },

            // 文件上传相关
            ResponseCodeEnum::FILE_NOT_FOUND => ResponseCode { code: 404, msg: "文件未找到".into(), error: "File Not Found".into() },
            ResponseCodeEnum::ERROR_FILE_TYPE => ResponseCode { code: 415, msg: "不支持的文件类型".into(), error: "Unsupported File Type".into() },
            ResponseCodeEnum::OVER_SIZE => ResponseCode { code: 413, msg: "文件超出大小限制".into(), error: "File Size Over Limit".into() },
            ResponseCodeEnum::UPLOAD_FAILED => ResponseCode { code: 500, msg: "文件上传失败".into(), error: "File Upload Failed".into() },
            ResponseCodeEnum::FILE_ALREADY_EXISTS => ResponseCode { code: 409, msg: "文件已存在".into(), error: "File Already Exists".into() },
            ResponseCodeEnum::UNSUPPORTED_FILE_ENCODING => ResponseCode { code: 415, msg: "不支持的文件编码".into(), error: "Unsupported File Encoding".into() },

            // 业务逻辑相关
            ResponseCodeEnum::BUSINESS_ERROR => ResponseCode { code: 400, msg: "业务逻辑错误".into(), error: "Business Logic Error".into() },
            ResponseCodeEnum::OPERATION_FAILED => ResponseCode { code: 500, msg: "操作失败".into(), error: "Operation Failed".into() },
            ResponseCodeEnum::DATA_CONFLICT => ResponseCode { code: 409, msg: "数据冲突".into(), error: "Data Conflict".into() },
            ResponseCodeEnum::RESOURCE_LOCKED => ResponseCode { code: 423, msg: "资源被锁定".into(), error: "Resource Locked".into() },
            ResponseCodeEnum::DEPENDENCY_ERROR => ResponseCode { code: 424, msg: "依赖错误".into(), error: "Dependency Error".into() },
            ResponseCodeEnum::INVALID_OPERATION => ResponseCode { code: 400, msg: "无效操作".into(), error: "Invalid Operation".into() },
            ResponseCodeEnum::RESOURCE_EXHAUSTED => ResponseCode { code: 507, msg: "资源耗尽".into(), error: "Resource Exhausted".into() },
            ResponseCodeEnum::PRECONDITION_FAILED => ResponseCode { code: 412, msg: "先决条件失败".into(), error: "Precondition Failed".into() },
            ResponseCodeEnum::FEATURE_NOT_IMPLEMENTED => ResponseCode { code: 501, msg: "功能未实现".into(), error: "Feature Not Implemented".into() },
            ResponseCodeEnum::RESOURCE_LIMIT_REACHED => ResponseCode { code: 429, msg: "达到资源限制".into(), error: "Resource Limit Reached".into() },
            ResponseCodeEnum::BUSINESS_VALIDATION_FAILED => ResponseCode { code: 400, msg: "业务验证失败".into(), error: "Business Validation Failed".into() },
            ResponseCodeEnum::DUPLICATE_REQUEST => ResponseCode { code: 409, msg: "重复请求".into(), error: "Duplicate Request".into() },
            ResponseCodeEnum::RATE_LIMIT_EXCEEDED => ResponseCode { code: 429, msg: "超出速率限制".into(), error: "Rate Limit Exceeded".into() },
            ResponseCodeEnum::UNSUPPORTED_OPERATION => ResponseCode { code: 405, msg: "不支持的操作".into(), error: "Unsupported Operation".into() },
            ResponseCodeEnum::ILLEGAL_ARGUMENT => ResponseCode { code: 400, msg: "非法参数".into(), error: "Illegal Argument".into() },
            ResponseCodeEnum::OPERATION_NOT_SUPPORTED => ResponseCode { code: 405, msg: "操作不支持".into(), error: "Operation Not Supported".into() },

            // 服务调用相关
            ResponseCodeEnum::SERVICE_TIMEOUT => ResponseCode { code: 504, msg: "服务超时".into(), error: "Service Timeout".into() },
            ResponseCodeEnum::SERVICE_ERROR => ResponseCode { code: 502, msg: "服务错误".into(), error: "Service Error".into() },
            ResponseCodeEnum::GATEWAY_ERROR => ResponseCode { code: 502, msg: "网关错误".into(), error: "Gateway Error".into() },
            ResponseCodeEnum::NETWORK_ERROR => ResponseCode { code: 503, msg: "网络错误".into(), error: "Network Error".into() },
            ResponseCodeEnum::EXTERNAL_SERVICE_ERROR => ResponseCode { code: 502, msg: "外部服务错误".into(), error: "External Service Error".into() },
            ResponseCodeEnum::DEPENDENCY_SERVICE_ERROR => ResponseCode { code: 502, msg: "依赖服务错误".into(), error: "Dependency Service Error".into() },
            ResponseCodeEnum::THIRD_PARTY_SERVICE_TIMEOUT => ResponseCode { code: 504, msg: "第三方服务超时".into(), error: "Third Party Service Timeout".into() },
            ResponseCodeEnum::THIRD_PARTY_SERVICE_ERROR => ResponseCode { code: 502, msg: "第三方服务错误".into(), error: "Third Party Service Error".into() },
            ResponseCodeEnum::INVALID_SERVICE_RESPONSE => ResponseCode { code: 502, msg: "无效的服务响应".into(), error: "Invalid Service Response".into() },
            ResponseCodeEnum::SERVICE_RATE_LIMIT_EXCEEDED => ResponseCode { code: 429, msg: "服务速率限制超出".into(), error: "Service Rate Limit Exceeded".into() },
            ResponseCodeEnum::SERVICE_NOT_FOUND => ResponseCode { code: 404, msg: "服务未找到".into(), error: "Service Not Found".into() },
            ResponseCodeEnum::SERVICE_NOT_CONFIG => ResponseCode { code: 500, msg: "服务未配置".into(), error: "Service Not Config".into() },
            ResponseCodeEnum::SERVICE_NOT_IMPLEMENTED => ResponseCode { code: 501, msg: "服务未实现".into(), error: "Service Not Implemented".into() },

            // 独立的网关状态码
            ResponseCodeEnum::GATEWAY_TIMEOUT => ResponseCode { code: 504, msg: "网关超时".into(), error: "Gateway Timeout".into() },
            ResponseCodeEnum::BAD_GATEWAY => ResponseCode { code: 502, msg: "错误的网关".into(), error: "Bad Gateway".into() },

            // 其他
            ResponseCodeEnum::UNKNOWN_ERROR => ResponseCode { code: 500, msg: "未知错误".into(), error: "Unknown Error".into() },
            ResponseCodeEnum::MAINTENANCE => ResponseCode { code: 503, msg: "维护中".into(), error: "Maintenance".into() },
            ResponseCodeEnum::OPERATION_NOT_ALLOWED => ResponseCode { code: 403, msg: "操作不允许".into(), error: "Operation Not Allowed".into() },
            ResponseCodeEnum::REQUEST_TIMEOUT => ResponseCode { code: 408, msg: "请求超时".into(), error: "Request Timeout".into() },
            ResponseCodeEnum::NOT_IMPLEMENTED => ResponseCode { code: 501, msg: "功能未实现".into(), error: "Not Implemented".into() },
        }
    }
}
